package com.silence.gankio.paged

import android.arch.lifecycle.MutableLiveData
import android.arch.paging.ItemKeyedDataSource
import com.silence.gankio.bean.GankIoData
import com.silence.gankio.core.net.HttpUtil
import com.silence.gankio.net.GankIoObserver
import com.silence.gankio.service.GankIoService
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers
import java.util.concurrent.Executor

class GankIoDataSource(private val retryExecutor: Executor): ItemKeyedDataSource<String, GankIoData>() {

    private var retry: (() -> Any)? = null
    private var startPosition: Int = 1

    fun retryAllFailed() {
        val prevRetry = retry
        retry = null
        prevRetry?.let {
            retryExecutor.execute{it.invoke()}
        }
    }

    val networkState by lazy {
        MutableLiveData<Resource<String>>()
    }

    val initialLoad by lazy {
        MutableLiveData<Resource<String>>()
    }

    override fun loadInitial(params: LoadInitialParams<String>, callback: LoadInitialCallback<GankIoData>) {
        networkState.postValue(Resource.longing(null))
        initialLoad.postValue(Resource.longing(null))

//        HttpUtil.getInstance().getService(GankIoService::class.java).android(startPosition, params.requestedLoadSize)
//            .subscribeOn(Schedulers.io())
//            .observeOn(AndroidSchedulers.mainThread())
//            .subscribe(object : GankIoObserver<GankIoData>(this){
//                override fun onSuccess(t: GankIoData) {
//
//                }
//            })

    }

    override fun loadAfter(params: LoadParams<String>, callback: LoadCallback<GankIoData>) {
    }

    override fun loadBefore(params: LoadParams<String>, callback: LoadCallback<GankIoData>) {
    }

    override fun getKey(item: GankIoData): String {
        return ""
    }
}